package com.linkivr.action;

import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;
import net.sf.ezmorph.bean.MorphDynaBean;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.stereotype.Component;

import com.linkivr.util.GL;
import com.linkivr.model.*;
import com.linkivr.service.*;


@Component("channelAction")
public class ChannelAction extends MyAction {
	private static final long serialVersionUID = 1L;
	private ChannelService service;
	private ModuleService moduleService;
	private FactorService factorService;
	private FunctionService functionService;

	public ChannelService getService() {
		return service;
	}

	@Resource
	public void setService(ChannelService service) {
		this.service = service;
	}

	public ModuleService getModuleService() {
		return moduleService;
	}

	@Resource
	public void setModuleService(ModuleService moduleService) {
		this.moduleService = moduleService;
	}

	public FactorService getFactorService() {
		return factorService;
	}

	@Resource
	public void setFactorService(FactorService factorService) {
		this.factorService = factorService;
	}

	public FunctionService getFunctionService() {
		return functionService;
	}

	public void setFunctionService(FunctionService functionService) {
		this.functionService = functionService;
	}

	public String list() throws IOException {
		GL.info("user.list()函数  ");
		beginReqParam();
		// 获取表达参数

		List<T_channel> tabObjs = service.queryAll(start, limit);

		jsonString = "{totalProperty:" + service.queryCount() + ",root:[";
		int i = 0;
		for (T_channel to : tabObjs) {

			if (i != 0 && i != tabObjs.size())
				jsonString = jsonString + ",";
			i++;
			String t = "{id:'" + to.getId() + "',chn_id:'" + to.getChn_id() + "',chn_name:'" + to.getChn_name()
					+ "',mod_name:'" + to.getModule().getMod_name() + "',fac_name:'" + to.getFactor().getFac_name()
					+ "',use_function:'" + to.getUse_function() + "',fun_name:'" + to.getFunction().getFun_name()
					+ "'}";
			jsonString = jsonString + t;
		}

		jsonString += "]}";
		out.write(jsonString);
		GL.info(jsonString);
		endReqParam();
		return null;
	}

	public String listData() throws IOException {
		GL.info("user.listData()函数  ");
		beginReqParam();
		// 获取表达参数

		List<T_channel> tabObjs = service.queryAll(start, limit);

		jsonString = "{totalProperty:" + service.queryCount() + ",root:[";
		int i = 0;
		for (T_channel to : tabObjs) {

			if (i != 0 && i != tabObjs.size())
				jsonString = jsonString + ",";
			i++;
			String t = "{id:'" + to.getId() + "',chn_name:'" + to.getChn_name() + "',lastData:'" + to.getLastData()
					+ "',updateTime:'" + to.getUpdateTime() + to.getLastData() + "',chn_state:'" + to.getChn_state()
					+ "',mod_name:'" + to.getModule().getMod_name() + "',com_name:'"
					+ to.getModule().getComport().getCom_name() + "',app_name:'"
					+ to.getModule().getComport().getApp().getApp_name() + "',unit_name:'"
					+ to.getModule().getComport().getApp().getUnit().getUnit_name() + "'}";
			jsonString = jsonString + t;
		}

		jsonString += "]}";
		out.write(jsonString);
		GL.info(jsonString);
		endReqParam();
		return null;
	}

	public String listAll() throws Exception {
		GL.info("listAll()函数  ");
		beginReqParam();
		// 获取表达参数
		// ApplicationContext ac = new
		// ClassPathXmlApplicationContext("beans.xml");
		JSONArray array = JSONArray.fromObject(service.queryAll());
		out.write(array.toString());
		endReqParam();
		return null;
	}

	public String query() throws IOException {
		GL.info("query()函数  ");
		String jsonString = "";
		beginReqParam();
		// 获取表达参数

		String p_id = request.getParameter("p_id");

		T_channel to = service.query(Integer.parseInt(p_id));
		GL.info("query()函数 ok p_id= " + p_id);
		jsonString = "{success: true ,data:{f_chn_id:'" + to.getChn_id() + "',f_chn_name:'" + to.getChn_name()
				+ "',f_mod_id:'" + to.getModule().getMod_id() + "',f_fac_id:'" + to.getFactor().getFac_id()
				+ "',f_use_function:'" + to.getUse_function() + "',f_fun_id:'" + to.getFunction().getFun_id() + "'}}";
		out.write(jsonString);
		GL.info(jsonString);
		endReqParam();
		return null;
	}

	public String add() throws IOException {
		GL.info("add()函数  ");
		String jsonString = "";
		beginReqParam();
		// 获取表达参数

		String f_chn_id = request.getParameter("f_chn_id");
		String f_chn_name = request.getParameter("f_chn_name");
		String f_mod_id = request.getParameter("f_mod_id");
		String f_fac_id = request.getParameter("f_fac_id");
		String f_use_function = request.getParameter("f_use_function");
		String f_fun_id = request.getParameter("f_fun_id");

		T_channel to = new T_channel();
		to.setChn_id(Integer.parseInt(f_chn_id));
		to.setChn_name(f_chn_name);
		to.setUse_function(Boolean.parseBoolean(f_use_function));

		T_module module = moduleService.query(Integer.parseInt(f_mod_id));
		to.setModule(module);
		T_factor factor = factorService.query(Integer.parseInt(f_fac_id));
		to.setFactor(factor);
		T_function function = functionService.query(Integer.parseInt(f_fun_id));
		to.setFunction(function);

		service.add(to);

		jsonString = "{success: true ,msg:'操作成功'}";
		out.write(jsonString);
		GL.info(jsonString);
		endReqParam();
		return null;
	}

	public String delete() throws IOException {
		GL.info("delete()函数  ");
		String jsonString = "";
		beginReqParam();
		// 获取表达参数

		String f_data = request.getParameter("f_data");
		JSONObject a = null;
		Object bean = null;
		JSONArray array = JSONArray.fromObject(f_data);
		Object[] objArray = array.toArray();
		for (Object obj : objArray) {
			a = JSONObject.fromObject(obj);
			bean = JSONObject.toBean(a);
			// 经过查看，发现 bean 为 MorphDynaBean 类型
			MorphDynaBean morphDynaBean = (MorphDynaBean) bean;
			String id = (String) morphDynaBean.get("id");
			service.delete(Integer.parseInt(id));

		}
		jsonString = "{success: true ,msg:'操作成功'}";
		out.write(jsonString);
		GL.info(jsonString);
		endReqParam();

		return null;
	}

	public String update() throws IOException {
		GL.info("update()函数  ");
		String jsonString = "";
		beginReqParam();
		// 获取表达参数

		String p_id = request.getParameter("p_id");

		String f_chn_id = request.getParameter("f_chn_id");
		String f_chn_name = request.getParameter("f_chn_name");
		String f_mod_id = request.getParameter("f_mod_id");
		String f_fac_id = request.getParameter("f_fac_id");
		String f_use_function = request.getParameter("f_use_function");
		String f_fun_id = request.getParameter("f_fun_id");

		T_channel to = service.query(Integer.parseInt(p_id));
		to.setChn_id(Integer.parseInt(f_chn_id));
		to.setChn_name(f_chn_name);
		to.setUse_function(Boolean.parseBoolean(f_use_function));

		T_module module = moduleService.query(Integer.parseInt(f_mod_id));
		to.setModule(module);
		T_factor factor = factorService.query(Integer.parseInt(f_fac_id));
		to.setFactor(factor);
		T_function function = functionService.query(Integer.parseInt(f_fun_id));
		to.setFunction(function);

		service.update(to);

		jsonString = "{success: true ,msg:'操作成功'}";
		out.write(jsonString);
		GL.info(jsonString);
		endReqParam();
		return null;
	}

}
